

new Vue({
    data:{
        caseCategory:[],
        loading:false,
        caseCategoryId : 2, //合作案例的id默认为2
        currentCategoryId:2, //当前分类id
        articles:[],
        paginate:{},
        currentPage: 1
    },

    methods:{

        //根据默认id请求分类列表
        getCaseCategory:function(){
            this.loading = true;
            var self = this;
            var response =  self.$http.get('/api/web-page/news-article/create/').then(function(response){
                return response.json();
            }).catch(function(error){
                console.error(error);
            });

            response.then(function(result){
                if(result.code !== 0){
                    throw new Error("请求分类列表失败，code:"+result.code);
                }

                self.caseCategory = _(result.tags).find(function (el) {
                    return el.id === self.caseCategoryId
                })['sub_tag'];

               // self.currentCategoryId = self.caseCategory[0].id; //第一项为当前默认
                self.loading = false;

                //请求新闻摘要列表数据
                self.getCaseList();
            })
        },

        //请求案例摘要列表
        getCaseList:function(){
            var self = this;
            self.loading = true;
            this.$http.get("/api/web-page/news-article?page="+self.currentPage+"&per_page=9&tag_id="+self.currentCategoryId).then(function(response){
                return response.json();
            }).then(function(result){

                if(result.code !== 0){
                    throw new Error('请求案例摘要列表失败，code: '+ result.code);
                }
                self.articles = result['articles_paginate'].data;
                delete result['articles_paginate'].data;
                self.paginate = result['articles_paginate'];
                self.loading = false;

            }).catch(function(error){
                console.error(error);
            })
        },

        //切换分页
        changePage:function(number){
            this.currentPage = Number(number);
            this.getCaseList();
        },

        //切换分类
        changeCategory:function( categoryId ){
            categoryId = Number(categoryId);
            if( categoryId === this.currentCategoryId ){
                return;
            }else{
                this.currentPage = 1;
                this.currentCategoryId = categoryId;
            }
            this.getCaseList();
        }
    },

    components:{
        bymPagination: bymPagination
    },

    mounted:function(){
        //请求案例数据
        this.getCaseCategory();
    }

}).$mount('#case');
